﻿<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/sharedfilecategory.tld" prefix="sfcat"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<script type="text/javascript" src="<%=request.getContextPath()%>/qa/js/catselect.js"></script>
<span>
<html:link page="/sf.mrqa?action=list">共享资料</html:link>&nbsp;-&nbsp;新增
</span>
<table class="mars">
    <html:form action="/sf" method="post" enctype="multipart/form-data" onsubmit="return validateSform(this);">
    <html:hidden property="action" value="add"/>
    <input type="hidden" name="catId"/>
   <tr class="even">
        <td align="right">附件</td>
        <td>
            <html:file property="file" size="70" onchange="fileChanged(this);"/>*(大小不能超过5M,建议英文文件名)
        </td>
    </tr>
    <tr>
        <td align="right">名称</td>
        <td>
            <html:text property="name" size="60" maxlength="40"/>*(如果不填写，默认为上载文件名字;最长40个字符)
        </td>
    </tr> 
    <tr class="even">
        <td align="right">图片</td>
        <td>
            <iframe name="ad" frameborder="0" width="100%" height="25" scrolling="no" src="/question.mrqa?action=uploadImage&imageIn=sf&imageArea=sharedFile[1].description"></iframe>
        </td>
    </tr> 
    <tr>
        <td align="right">分类</td>
        <td valign="top">
            <html:select property="cat1Id" multiple="true" size="15" onchange="selectedCat1Changed(this)">
            </html:select>
            <html:select property="cat2Id" multiple="true" size="15" onchange="selectedCat2Changed(this)">
                <html:option value="">选择分类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
            </html:select>
            <html:select property="cat3Id" multiple="true" size="15">
                <html:option value="">选择分类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
            </html:select>*
        </td>
    </tr>
    <tr class="even">
        <td align="right">描述</td>
        <td>
            <html:textarea property="description" cols="50" rows="5"/>(最长200个字符)
        </td>
    </tr> 
    <tr>
        <td align="right"></td>
        <td>
            验证码<html:text property="vc" size="4" maxlength="4" value=""/>*(输入右边图片上的数字)<img src="<%=request.getContextPath()%>/vcimg" width="40" height="20" border="0" align="absbottom"/>
            &nbsp;&nbsp;
            <c:if test="${disableSubmit == 'true'}">
            	<input type="submit" value="保存共享资料" disabled/>
            </c:if>
            <c:if test="${disableSubmit != 'true'}">
            	<input type="submit" value="保存共享资料"/>
            </c:if>
        </td>
    </tr>
    </html:form>
</table>
<xml id="catsData">
      <cats id="cats">  
        <sfcat:insert resultKind="dropdown"/>
      </cats>
</xml>
<script type="text/javascript">
    /**
    *the file name changes according to selected local file
    */
    function fileChanged(pFile)
    {
        var name = pFile.form.name.value;
        if(name.length > 0)return;
        var fileName = pFile.value;
        var slashLastIndex = fileName.lastIndexOf("\\");
        var dotIndex = fileName.lastIndexOf(".");
        var shortFileName = fileName.substring(slashLastIndex+1,dotIndex);
        pFile.form.name.value=shortFileName;
    }
    try
    {
        var sharedFileForms = document.sharedFile;
        var sharedFileForm;
        if(sharedFileForms.length > 1)
        {
            for(var i=0;i<sharedFileForms.length;i++)
            {
                sharedFileForm = sharedFileForms[i];
                if(sharedFileForm.action == "add")
                {
                    break;
                }
            }
        }
        initSelect1ForSF(sharedFileForm);
    }
    catch(err){}
    function validateSform(pForm)
    {
        var cat1IdValue = pForm.cat1Id.options[pForm.cat1Id.selectedIndex].value;
        var cat1IdText = pForm.cat1Id.options[pForm.cat1Id.selectedIndex].text;
        var cat2IdValue = pForm.cat2Id.options[pForm.cat2Id.selectedIndex].value;
        var cat2IdText = pForm.cat2Id.options[pForm.cat2Id.selectedIndex].text;
        var cat3IdValue = pForm.cat3Id.options[pForm.cat3Id.selectedIndex].value;
        var cat3IdText = pForm.cat3Id.options[pForm.cat3Id.selectedIndex].text;
        var selectedCatIdText;
        var selectedCatIdValue;
        if(cat3IdValue.length > 0)
        {
            selectedCatIdValue = cat3IdValue;
            selectedCatIdText = cat3IdText;
        }
        else
        {
            if(cat2IdValue.length > 0)
            {
                selectedCatIdValue = cat2IdValue;
                selectedCatIdText = cat2IdText;
            }
            else
            {
                if(cat1IdValue.length > 0)
                {
                    selectedCatIdValue = cat1IdValue;
                    selectedCatIdText = cat1IdText;
                }
                else
                {
                    selectedCatIdValue = "";
                    selectedCatIdText = "";
                }
            }
        }
        if(selectedCatIdValue == "")
        {
            alert("[分类]必须选择");
            return false;
        }
        pForm.catId.value = selectedCatIdValue;
        if(pForm.file.value == "")
        {
            alert("[文件]必须选择");
            pForm.file.focus();
            return false;
        }
        if(pForm.name.value == "")
        {
            alert("[名称]必须填写");
            pForm.name.focus();
            return false;
        }
        else
        {
            if(pForm.name.value.length < 2)
            {
                alert("[名称]必须大于2个字符");
                pForm.name.focus();
                return false;
            }
            else if(pForm.name.value.length > 40)
            {
                alert("[名称]最长40个字符");
                pForm.name.focus();
                return false;
            }
        }
        if(pForm.description.value != "" && pForm.description.value.length > 200)
        {
            alert("[描述]不能大于200个字符!");
            pForm.description.focus();
            return false;
        }
        if(pForm.vc.value == "" || pForm.vc.value.length < 4)
        {
            alert("[验证码]必须填写正确");
            pForm.vc.focus();
            return false;
        }
        var uploadImage = frames["ad"].window.document.getElementById("uploadButton");
        if(uploadImage){
    			uploadImage.disabled = true;
    		}
        return true;
    }
</script>
